import pymysql


class Operation(object):
    def __init__(self):
        self.db = pymysql.connect(host="localhost", user="root",
                                  password="xj20010126", db="student", port=3306)

    def cur(self, action):
        cur = self.db.cursor(action)
        return cur

    @staticmethod
    def get_table(table_name):
        sql = 'select * from ' + table_name
        return sql


if __name__ == '__main__':
    op = Operation()
    table_name = '爬虫'
    get_table = op.get_table(table_name)
    cur = op.cur(get_table)
    results = cur.fetchall()
    print('id', 'title', 'data', 'writer', 'url')
    for row in results:
        id = row[0]
        title = row[1]
        writer = row[3]
        date = row[2]
        url = row[4]
        print(id, title, writer, date, url)
try:
    # cur.execute(sql)

    results = cur.fetchall()
    print('id', 'title', 'data', 'writer', 'url')
    for row in results:
        id = row[0]
        title = row[1]
        writer = row[3]
        date = row[2]
        url = row[4]
        print(id, title, writer, date, url)

except Exception as e:
    raise e
# finally:
    # db.close()
db = pymysql.connect(
    host="localhost",
    user="root",
    password="xj20010126",
    db="student",
    port=3306)
cur = db.cursor()
sql = 'select * from 爬虫'
try:
    cur.execute(sql)

    results = cur.fetchall()
    print('id', 'title', 'data', 'writer', 'url')
    for row in results:
        id = row[0]
        title = row[1]
        writer = row[3]
        date = row[2]
        url = row[4]
        print(id, title, writer, date, url)

except Exception as e:
    raise e
finally:
    db.close()

# sql_insert = """insert into 爬虫(title,writer,url) values({},{},{},{})""".format(dic['title'])
try:
    # cur.execute(sql_insert)
    db.commit()
except Exception as e:
    db.rollback()
finally:
    db.close()
